function bci=bciGenerateEvents(bci,callingFunc)
% bci=bciGenerateEvents(bci,callingFunc)
% creates and modifys event vectors dependent on paradigm
% callingFunc currently can be 'init' 'train' 'test'

if bci.paradigm.id<10,
    if strfind(callingFunc,'init');
        % event vector creation
        bci.movEvents = intersect(bci.eventsToClassify,[bci.leftEvent,bci.rightEvent]);
        if bci.paradigm.id==4,
            if length(bci.movEvents)>1,
                error('This paradigm allows only one movement condition');
            end
            eventVec = repmat([6,bci.baseEvent,6,bci.movEvents],1,bci.numTrials);
        elseif length(bci.movEvents)==1,
            eventVec = repmat([bci.baseEvent,bci.movEvents],1,bci.numTrials);
        elseif length(bci.movEvents)==2,
            if  any(bci.eventsToClassify==bci.baseEvent),
                eventVec = repmat([bci.baseEvent,bci.movEvents(1),bci.baseEvent,bci.movEvents(2)],1,bci.numTrials);
            else
                eventVec = repmat(bci.movEvents,1,bci.numTrials);
            end
        else
            error('eventsToClassify configuration not implemented');
        end
        if bci.paradigm.id<4,
            % add pause events
            idx=find(eventVec==bci.leftEvent | eventVec==bci.rightEvent);
            idx = idx(10:10:end-10);
            for k=1:length(idx),
                tmp=eventVec(idx(k):end);
                eventVec=[eventVec(1:idx(k)-1) [5 6] tmp];
                idx=idx+2;
            end
        end
        % add start and finish
        bci.events = [ 7 eventVec 4];
        bci.eventDur = zeros(size(bci.events));
        bci.eventDur(1)=4;
        idx = find(bci.events==bci.leftEvent|bci.events==bci.rightEvent);
        movDurations = repmat(bci.movDur,1,ceil(length(idx)/length(bci.movDur)));
        bci.eventDur(idx)=movDurations(1:length(idx));
        bci.eventDur(bci.events==bci.baseEvent)=bci.fixDur;
        bci.eventDur(bci.events==5)=8;
        bci.eventDur(bci.events==6)=2;
    elseif strfind(callingFunc,'train'),% case train run:
        if bci.paradigm.id==4,
            idx=find(bci.events==bci.leftEvent | bci.events==bci.rightEvent| bci.events==bci.baseEvent);
            bci.events(idx)=bciScrambleEvents(bci.events(idx),3);
        else
            if length(bci.movEvents)>1 && length(bci.eventsToClassify)>2,
                movEventIdx=find(bci.events==bci.leftEvent | bci.events==bci.rightEvent);
                % scramble with at most 3 successive equal events
                bci.events(movEventIdx) = bciScrambleEvents(bci.events(movEventIdx),3);
            end
        end
    elseif strfind(callingFunc,'test'),% case feedback run:
        % generate 40 events
        if length(bci.movEvents)>1,
            bci.events = bciScrambleEvents([bci.leftEvent*ones(1,20),bci.rightEvent*ones(1,20)],2);
            % add unsupervised 60 events (not expected to need this)
            bci.events=[bci.events,2*double(rand(1,60)<0.5)+1];
            bci.eventDur = zeros(size(bci.events));
        else
            if bci.paradigm.id==4,
                evt=bci.events;
                evt([1,end])=[];
                bci.eventDur([1,end])=[];
                idx=find(evt==bci.leftEvent | evt==bci.rightEvent| evt==bci.baseEvent);
                evt(idx)=bciScrambleEvents(evt(idx),3);
                bci.events=repmat(evt,1,ceil(100/length(idx)));
                bci.eventDur=repmat(bci.eventDur,1,ceil(100/length(idx)));
            else
                bci.events = bci.movEvents * ones(1,100);
                bci.eventDur = zeros(size(bci.events));
            end
        end
    else
        error('calling Function unknown');
    end
elseif bci.paradigm.id >10, % since bciInit is called with each Run, randomization at each call 
    nObjects = length(bci.eventsToClassify);
    if bci.numTrials ==1 && bci.paradigm.feedbackType == 2 && strcmpi(bci.paradigm.objectAlignment,'o'),
        evtVector = 1; % only one single trial in object recognition + gripper mode
    else
        evtVector = reshape([1:nObjects]'*ones(1,bci.numTrials),1,nObjects*bci.numTrials);
    end
    randIdx=randperm(length(evtVector));
    evtVector=evtVector(randIdx);
    % evtVector defines reordering, evt 8 defines start of flicker
    events=[evtVector; ones(1,length(evtVector))*8];
    % fixDur defines orientation interval movDur defines flicker interval
    eventDur = [ones(1,length(evtVector))*bci.fixDur;ones(1,length(evtVector))*bci.movDur];
    bci.events=[7 events(:)' 10];
    bci.eventDur = [4 eventDur(:)' 0];
else
    error('Paradigm id not defined.');
end